var db = require("../models");

exports.findUserById = function(_userId, callback){
	db.User.findOne({_id: _userId}, callback);
}

exports.LoginUser = function(user, callback){
	db.User.findOne({
		name: user.name,
		password: user.password
	}, callback)
}

exports.CreateUser = function(user, callback){
	var oner = new db.User;
	oner.name = user.name;
	oner.password = user.password;
	oner.headUrl = "http://static.vrbig.com/net/head/header-default.jpg";
	oner.online = false;
	oner.save(callback);
}

exports.getOnlineUsers = function(callback){
	db.User.find({
		online: true
	}, callback)
}

exports.online = function(_userId, callback){
	db.User.findOneAndUpdate({
		_id: _userId
	}, {
		$set: {
			online: true
		}
	}, callback)
}
exports.offline = function(_userId, callback){
	db.User.findOneAndUpdate({
		_id: _userId
	}, {
		$set: {
			online: false
		}
	}, callback)
}
exports.joinRoom = function(data, callback){
	db.User.findOneAndUpdate({
		_id: data.user._id
	}, {
		$set: {
			online: true,
			_roomId: data.room._id
		}
	}, callback)
}
exports.leaveRoom = function (leaver, callback) {
	db.User.findOneAndUpdate({
	    _id: leaver.user._id
	  }, {
	    $set: {
		    online: true,
		    _roomId: null
	    }
	}, callback)
}































